Previous Post: JavaScript Essentials - Part I
A switch case evaluates the expression. A matched case will run until a break (or the end of the switch statement) is found.<b/r>
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
switch (new Date()
.getDay()) {
case 1:
console.log("Monday working day.");
break;
case 2:
console.log("Tuesday working day.");
break;
case 3:
console.log("Wednesday working day.");
break;
case 4:
console.log("Thursday government holiday.");
break;
case 5:
console.log("Friday is a last working day of the week.");
break;
case 6:
console.log("Saturday week end.");
break;
case 7:
console.log("Sunday week end.");
break;
default:
console.log("No any case found.");
// last line end of the case statement, no need to write break; keyword
}
Output:
1
Tuesday working day
Variables declared with var, let & const have the global scope and functional/local scope. Once defined globally var is available to use in the whole window and if defined in the function, can be used in the scope of the function.
1
2
3
4
5
6
7
8
9
//global scope
var name = 'jay'
testfunc = () => {
//local scope
var name = 'hey'
console.log(name);
}
testfunc()
console.log(name);
Output:
1 2
hey jay
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//gloabal scope for var,let,const
var num1 = 500;
let num2 = 600;
const num3 = 700;
numFunc = () => {
// local/functional scope for var,let,const
var num1 = 800;
let num2 = 900;
const num3 = 1000;
console.log("inside numFunc ", num1, num2, num3)
}
numFunc()
console.log("outside numFunc ", num1, num2, num3)
Output:
1
2
inside numFunc 800 900 1000
outside numFunc 500 600 700
As var can be used anywhere in the code, it may cause issues, as you may not know whether the variable is already declared. Let and const are the ES6 features. Let is an improved version of var.
Let is block scope. Any variable declared in the {} (curly) braces, can only be used inside that block.
1
2
3
4
5
6
7
8
9
10
11
12
var num1 = 500;
let num2 = 600;
const num3 = 700;
if (1) {
var num1 = 800;
let num2 = 900;
const num3 = 1000;
console.log("inside if ", num1, num2, num3)
}
console.log("outside if ", num1, num2, num3)
Output:
1
2
3
4
inside
if 800 900 1000
outside
if 800 600 700
As you can see in the output the var declared variable num1 is assigned with the latest value, let and const declared values are blocked scope. Variables declared with let can be updated in a different scope. If you try and redefine the variable with let in the same scope, it will throw syntax errors. Variables declared with const can neither be updated nor re-defined.
1 2 3
let name = 'world'; let name = 'helloworld'; console.log(name)
Output:
1
Uncaught SyntaxError: Identifier‘ name’ has already been declared
Hoisting is a JavaScript mechanism where functions and variables are moved to the top of their scope of execution.
JavaScript only hoists the declarations and not the initializations. If the variable is declared and used before initializing it, then it will return undefined.
1
2
3
console.log(data); // undefined
var data;
data = 10;
The string is used for storing and manipulating the text. We will see some of the string methods.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const name = 'joHn DoE';
const age = 30;
const address = 'Vasant Vihar New Delhi';
const fruits = 'apple,banana,orange,watermelon';
let output;
// finding a string in string
output = name.indexOf('h');
console.log(output); //-1
output = name.lastIndexOf('D');
console.log(output); // 5
// Capitalization and lowercase
output = name.toUpperCase();
console.log(output); //JOHN DOE
output = name.toLowerCase();
console.log(output); //john doe
// Return a character with speified index with charAt() method
output = name.charAt('6');
console.log(output); //o
// Get last character of the string
output = name.trim()
.charAt(name.length - 1);
console.log(output); //E
// replace()
output = name.replace('joHn', 'DoE');
console.log(output); //DoE DoE
output = name.replace('DoE', 'joHn');
console.log(output); //joHn joHn
// includes() method will check whether the string includes the given string
output = address.toLocaleLowerCase()
.includes('delhi');
console.log(output); //true
// substring() method does not take the negative values unlike slice() method
output = name.substring(1, 7);
console.log(output); //oHn Do
// slice() method can take both positive and negative values as the param
output = name.slice(1, 5);
console.log(output); //oHn
output = name.slice(-3);
console.log(output); //DoE
// split()
output = address.split(' ');
console.log(output); //(4) ["Vasant", "Vihar", "New", "Delhi"]
output = fruits.split(',');
console.log(output); //(4) ["apple", "banana", "orange", "watermelon"]
In this article, we saw some of the essential concepts of JavaScript which are very important for any developer to begin with the JavaScript language. I hope you now have a good understanding of these important concepts.
In the next article, we shall discuss more concepts of JavaScript, until then feel free to explore the world of JavaScript.